<?php
/**
 * 权限类
 * @author sundongdong
 *
 */
class Application_Model_Privilege extends Model_Base {
	/**
	 * 根据名称获取用户信息
	 * @param unknown $username
	 * @param string $filed
	 */
	public function getUserByName($username, $filed = "*") {
		$select = $this->db()->select();
		$select->from('s_user', $filed)->where('user_name = ?', $username);
		return $this->db()->fetchRow($select);
	}
	/**
	 * 获取用户的权限节点列表
	 * @param number $user_id
	 */
	public function getUserPrivilege($user_id = 0) {
		// 获取用户角色
		$roles = $this->db()->fetchCol("select role_id from s_user_role where user_id=:user_id", array('user_id' => $user_id));
		$auth = array();
		foreach ($roles as $role) {
			$temp = $this->db()->fetchCol('select element_id from s_auth where role_id=:role_id', array('role_id' => $role));
			$auth = array_unique(array_merge($auth, $temp));
		}
		return $auth;
	}
	public function getMenuId($module, $controller, $action) {
		$param = array(
			'module' => $module,
			'controller' => $controller,
			'action' => $action,
		);
		return $this->db()->fetchOne("select menu_id from s_menu where module=:module and controller=:controller and action=:action", $param);
	}
	/**
	 * 获取页面的导航
	 * @param unknown $module 模块名
	 * @param unknown $controller 控制器
	 * @param unknown $action 方法
	 * @return mixed[]|unknown[][]|mixed[][]
	 */
	public function getBreads($module, $controller, $action) {
		$return = array();
		$where = " fid>0 ";
		$where .= " and module = :module ";
		$where .= " and controller = :controller ";
		$where .= " and action = :action";
		$param = array(
			'module' => $module,
			'controller' => $controller,
			'action' => $action,
		);

		$last = $this->db()->fetchRow("select name,fid from `s_menu` where $where order by menu_id desc", $param);
		if ($last) {
			if ($last['fid']) {
				$mid = $this->db()->fetchRow("select name,fid from `s_menu` where menu_id={$last['fid']}");
			}

			if ($mid['fid']) {
				$first = $this->db()->fetchRow("select * from `s_menu` where menu_id={$mid['fid']}");
			}

			$return = array('first' => array('menu_name' => $first['name'], 'module' => $first['module'], 'controller' => $first['controller'], 'action' => $first['action']), 'mid' => $mid['name'], 'last' => $last['name']);
		}
		return $return;
	}
	/**
	 * 根据父ID获取菜单
	 * @param number $pid
	 */
	public function getMenuByPid($pid = 0) {
		return $this->db()->fetchAll("SELECT * FROM `s_menu` WHERE fid = :fid order by menu_id", array('fid' => $pid));
	}
	/**
	 * 获取用户的常用菜单
	 * @param number $user_id
	 */
	public function getCustomMenuId($user_id = 0) {
		return $this->db()->fetchAll("SELECT * FROM `s_user_custom_menus` WHERE user_id = :user_id", array('user_id' => $user_id));
	}
	/**
	 * 根据菜单ID获取用户常用菜单
	 * @param array $customMenuId
	 */
	public function getCustomMenu(array $customMenuIds) {
		if (!empty($customMenuIds)) {
			$menu_arr = array();
			foreach ($customMenuIds as $menu) {
				$menu_arr[] = $this->db()->fetchRow("SELECT name,module,controller,action,fid FROM `s_menu` WHERE menu_id=:menu_id order by menu_id", array('menu_id' => $menu['menu_id']));
			}
			return $menu_arr;
		}
		return '';
	}
	/**
	 * 获取所有菜单
	 */
	public function getAllMenu() {
		return $this->db()->fetchAll("select * from `s_menu` order by menu_id desc");
	}
	/**
	 * 更新用户的快捷菜单
	 * @param string $menu_ids
	 * @param number $user_id
	 */
	public function updateCustomMenu($menu_ids = '', $user_id = 0) {
		try {
			$this->db('write_db')->beginTransaction();
			$this->db('write_db')->delete('s_user_custom_menus', 'user_id=' . $user_id);
			$menu_ids = explode(',', $menu_ids);
			$data = array();
			foreach ($menu_ids as $menu_id) {
				$data[] = array(
					'user_id' => $user_id,
					'menu_id' => $menu_id,
				);
			}
			$this->insertBatch($this->db('write_db'), 's_user_custom_menus', $data);
			$this->db('write_db')->commit();
			return array('ret' => 1, 'msg' => '保存成功！');
		} catch (Exception $e) {
			$this->db('write_db')->rollBack();
			return array('ret' => 0, 'msg' => $e->getMessage());
		}
	}

	/**
	 * 通过user_id取其角色
	 * @return Array
	 */
	public function getUserRolesByUserId($userID) {

		$sql = 'SELECT r.role_name, r.role_id FROM s_user_role ur LEFT JOIN s_role r ON ur.role_id = r.role_id WHERE ur.user_id = ' . $userID;
		$role = $this->db()->fetchAll($sql);
		foreach ($role as $key => $values) {
			$roles[$values['role_id']] = $values;
		}
		return $roles;
	}
}